/* 
 *  This file is part of hmvcBlog-authentification.
 *
 *  hmvcBlog-authentification is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-authentification is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-authentification.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client;

import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.login.LoginController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.login.LoginView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.login.events.in.ShowLoginViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.logout.LogoutController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.logout.LogoutView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.logout.events.in.ShowLogoutViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.register.RegisterController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.register.RegisterView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.register.events.in.ShowRegisterViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.framework.client.module.AModuleControllerRegistry;
import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.event.IEvent;

/**
 * 
 *
 * @author jens.meiss
 */
public class Authentification extends AModuleControllerRegistry {

	public void handleEventImpl(IEvent event) {
	}

	public void initImpl() {
	}

	public void terminateImpl() {
	}

	public void onHistoryChanged(String historyToken) {
		
		if (historyToken.equals(HistoryToken.login)) {
			showLogin();
		} else if (historyToken.equals(HistoryToken.register)) {
			showRegister();
		} else if (historyToken.equals(HistoryToken.logout)) {
			showLogout();
		}
		
	}

	/**
	 * 
	 */
	private void showLogout() {
		
		if (!getComponent().containsChildComponent(LogoutController.class)) {
			addChildComponent(new Component(new LogoutView(), new LogoutController()));
		}
		
		fireEventDown(new ShowLogoutViewEvent());
		
	}

	/**
	 * 
	 */
	private void showRegister() {
		
		if (!getComponent().containsChildComponent(RegisterController.class)) {
			addChildComponent(new Component(new RegisterView(), new RegisterController()));
		}
		
		fireEventDown(new ShowRegisterViewEvent());
		
	}

	/**
	 * 
	 */
	private void showLogin() {
		
		if (!getComponent().containsChildComponent(LoginController.class)) {
			addChildComponent(new Component(new LoginView(), new LoginController()));
		}
		
		fireEventDown(new ShowLoginViewEvent());
		
	}

}
